
function ajax(obj) { //{ url, data, method}
  var url = obj.url
  var data = obj.data || {}
  var method = obj.method || 'GET'
  return new Promise((resolve,reject)=>{
    wx.request({
      url: url, // 仅为示例，并非真实的接口地址
      data: data,
      method: method,
      header: {
        'content-type': 'application/texts' // 默认值
      },
      success(res) {
        if (res.statusCode != 200) return
        resolve(res.data)
      },
      fail(err){
        reject(err)
      }
    })
  })
}

function handleMovieIndexData(res){ //处理电影首页数据
  var list= []
  list.total = res.total
  if(res.subjects.length === 0) return list
   var subjects= res.subjects
  subjects.forEach((item,i) => {
    var obj= {}
    obj.image = item.images.large || item.images.medium || item.images.small || ''
    obj.id= item.id
    obj.title= item.title
    obj.year= item.year
    obj.rating= {}
    obj.rating.average= item.rating.average
    obj.rating.arr= []
    var count= Math.floor(item.rating.average/2)
    for(var i=0; i< 5; i++){
      if(i < count){
         obj.rating.arr.push(1)
      }else{
        obj.rating.arr.push(0)
      }
    }
      list.push(obj)
      // if(list.length >= 3) return true
  })
    
   return list  
}
function handleMovieDetail(res){
  var obj= {}
  obj.title = res.title
  obj.id = res.id
  obj.wish_count = res.wish_count
  obj.year = res.year
  obj.ratings_count = res.ratings_count
  obj.reviews_count = res.reviews_count
  obj.summary = res.summary
  obj.images = res.images.large || res.images.medium || res.images.small || ''
  obj.genres = res.genres.join('、')
  obj.rating = {}
  obj.rating.average = res.rating.average
  obj.rating.arr = []
  var count = Math.floor(res.rating.average / 2)
  for (var i = 0; i < 5; i++) {
    if (i < count) {
      obj.rating.arr.push(1)
    } else {
      obj.rating.arr.push(0)
    }
  }
  obj.url= []
  obj.casts= []
  var arr2= []
  for(var i= 0; i< res.casts.length; i++){
    var obj2 = {}
    obj2.id = res.casts[i].id
    obj2.name = res.casts[i].name
    obj2.avatar = res.casts[i].avatars.large || res.casts[i].avatars.medium || res.casts[i].avatars.small || ''
    obj.url.push(res.casts[i].avatars.large || res.casts[i].avatars.medium || res.casts[i].avatars.small || '')
    obj2.name_en = res.casts[i].name_en
    obj.casts.push(obj2)
    arr2.push(res.casts[i].name)
  }
  obj.castsName = arr2.join('/')
  obj.countries = res.countries.join('&')
  // obj.directors= 
  var arr1 = []
  res.directors.forEach((item,i)=> {
   arr1.push(item.name)
  })
  obj.directors = arr1.join('/')
  obj.original_title = res.original_title || res.title
  return obj
}
export {
  ajax,
  handleMovieIndexData,
  handleMovieDetail
}